<?php

include_once 'Super_Model_Admin.php';
include_once '../entity/admin/AdminUsers.php';

class Admin_Model_AdminUsers extends Super_Model_Admin {

    function getUsers(array $qAr) {

        $sql = $this->getEntityManager()->createQueryBuilder();
        $sql->add('select', 'u')
                ->add('from', 'AdminUsers u')
                ->add('orderBy', 'u.' . addslashes($qAr['order_field']) . ' ' . addslashes($qAr['order_dir']));

        $query = $sql->getQuery();
        $result = $query->getArrayResult();
        //$result = $this->getEntityManager()->getRepository( 'Users' )->findBy(array(), array('id' => 'ASC'), 100, 0);
        if ($result) {
            return $result;
        } else {
            return false;
        }
    }

    function getUsersById($id) {

        $sql = $this->getEntityManager()->createQueryBuilder();
        $sql->add('select', 'u')
                ->add('from', 'AdminUsers u')
                ->add('where', 'u.id = ?1')
                ->setParameter(1, intval($id));
        $query = $sql->getQuery();
        $result = $query->getArrayResult();
        if ($result) {
            return $result[0];
        } else {
            return false;
        }
    }

    function getUsersByLogin($login) {

        $sql = $this->getEntityManager()->createQueryBuilder();
        $sql->add('select', 'u')
                ->add('from', 'AdminUsers u')
                ->add('where', 'u.login = ?1')
                ->setParameter(1, addslashes($login));
        $query = $sql->getQuery();
        $result = $query->getArrayResult();
        if ($result) {
            return $result[0];
        } else {
            return false;
        }
    }

    function removeUser($id) {
        $em = $this->getEntityManager();
        $user = $em->getRepository('AdminUsers')->findOneBy(array('id' => intval($id)));
        if (!empty($user)) {
            $em->remove($user);
            $em->flush();
        }
    }

    function getAllAdminUsers() {
        $em = $this->getEntityManager();
        return $em->getRepository('AdminUsers')->findBy(array(), array('role_id' => 'ASC'));
    }
    
    public function getAllAdminUsersEx() {
        $users = $this->getAllAdminUsers();
        if(!empty($users) && count($users)){
            $result = array();
            foreach ($users as $mng){
                $result[$mng->id] = $mng->login;
            }
            return $result;
        }
        return false;
    }

    function getAdminLoginByAdminID($admin_id) {
        $em = $this->getEntityManager();
        $res = $em->getRepository('AdminUsers')->findOneBy(array('id' => (int) $admin_id));
        return ($res) ? $res->login : '';
    }

    function getRole() {
        $sql = $this->getEntityManager()->createQueryBuilder();
        $sql->add('select', 'u')
                ->add('from', 'AdminUsers u');
        $sql->add('groupBy', 'u.role');

        $query = $sql->getQuery();
        $temp = $query->getArrayResult();
        if ($temp) {
            $result = array();
            foreach ($temp as $val) {
                $result[] = $val['role'];
            }
            return $result;
        } else {
            return false;
        }
    }

    function updateUser(array $data) {
        $temp = array();
        $id = (int) $data['id'];
        unset($data['id'], $data['event']);
        if (isset($data) && is_array($data) && count($data)) {
            if (!empty($data['role_id'])) {
                $AGM = new Admin_Model_AdminGroups();
                $groupName = $AGM->getGroupsNameById($data['role_id']);
                if (!empty($groupName))
                    $data['role'] = $groupName;
            }
            foreach ($data as $key => $val) {
                $temp[] = ' t.' . $key . ' = \'' . addslashes(trim($val)) . '\'';
            }

            $query = "UPDATE AdminUsers t SET " . (implode(',', $temp)) . " WHERE t.id = " . $id;
            $s = $this->getEntityManager()
                    ->createQuery($query)
                    ->getResult();
            return $s;
        }
        return false;
    }

    public function insertUser(array $data) {
        $Currency = new AdminUsers();
        unset($data['event']);
        if (!empty($data['role_id'])) {
            $AGM = new Admin_Model_AdminGroups();
            $groupName = $AGM->getGroupsNameById($data['role_id']);
            if (!empty($groupName))
                $data['role'] = $groupName;
        }
        foreach ($data as $key => $val) {
            $Currency->{$key} = $val;
        }
        $em = $this->getEntityManager();
        $em->persist($Currency);
        $em->flush();
        return $Currency->id;
    }

    function getCountUsersByLogin($login) {
        $sql = $this->getEntityManager()->createQueryBuilder();
        $sql->add('select', 'count(u.login)')
                ->add('from', 'AdminUsers u')
                ->add('where', 'u.login = ?1')
                ->setParameter(1, trim($login));
        $query = $sql->getQuery();
        $result = $query->getResult();
        return isset($result) ? $result[0][1] : false;
    }

    public function writeRoleId() {
        $AGM = new Admin_Model_AdminGroups();
        $temp = $AGM->getRole();
        $roles = array();
        foreach ($temp as $tmp) {
            $roles[$tmp['title']] = $tmp['id'];
        }
        $sql = $this->getEntityManager()->createQueryBuilder();
        $sql->add('select', 'u')
                ->add('from', 'AdminUsers u');

        $query = $sql->getQuery();
        $users = $query->getArrayResult();
        foreach ($users as $user) {
            $em = $this->getEntityManager();
            $userUP = $em->getRepository('AdminUsers')->findOneBy(array('id' => intval($user['id'])));
            if (!empty($userUP)) {
                $userUP->role_id = $roles[trim($userUP->role)];
                $em->persist($userUP);
                $em->flush();
            }
        }
    }

    public function writeLastActiveAdminUsers($user_id) {
        if (!empty($user_id)) {
            $em = $this->getEntityManager();
            $item = $em->getRepository("AdminUsers")->findOneBy(array("id" => $user_id));
            if (!empty($item)) {
                $item->last_active = new DateTime();
                $em->persist($item);
                $em->flush();
                return true;
            }
        }
        return false;
    }

}